"""
蒙提霍尔问题（三门问题）模拟。
编号，car = 1, goat_1 = 2, goat_2 = 3。
"""

import random
import config

COUNT = 0
WIN_COUNT = 0
LOSE_COUNT = 0

while COUNT < config.COUNT:
    if COUNT % 1000000 == 0:
        print(f"Has been played {COUNT} times.")
    door_list = [1, 2, 3]
    first_choice = random.choice(door_list)
    door_list.remove(first_choice)

    if 1 in door_list:
        WIN_COUNT += 1
        with open('log.txt', 'a', encoding='utf-8') as file:
            WIN_RESUL = f"Total: {COUNT+1}, subtotal: {WIN_COUNT}, result: win\n"
            file.write(WIN_RESUL)
    else:
        LOSE_COUNT += 1
        with open('log.txt', 'a', encoding='utf-8') as file:
            LOSE_RESUL = f"Total: {COUNT+1}, subtotal: {LOSE_COUNT}, result: lose\n"
            file.write(LOSE_RESUL)
    COUNT += 1

conclusion = f"Total: {COUNT}, subtotal: {WIN_COUNT}, result: {(WIN_COUNT * 100) / COUNT:.2f}%"
print(conclusion)

with open('log.txt', 'a', encoding='utf-8') as file:
    file.write(f"\n{conclusion}")